"use strict";
define(function(require, exports, module) {
	let UI = require('core/ui'); 
	let controls = require('core/controls');
	let config = require('../config');
	let Service = require('core/service').Service;
	let UserService = require('./user.service');
	let service = new UserService();

	class UserView extends UI.EditListView {
		constructor() {
			super("main");
			this._service = new UserService().proxy(); 
			this.name = "用户";
		}

		get service() {
			return this._service;
		}

		getToolbars() {
			return {
				id: 'toolbar',
				data: [...controls.toolbars.listBar]
			}
		}

		getSearchControls() {
			return {
				controls: [{
					"label": "编码",
					"type": "textbox",
					"name": "userNo",
					"options": {
						"width": 150,
						"required": false
					}
				},{
					"label": "名称 ",
					"type": "textbox",
					"name": "name",
					"options": {
						"width": 150,
						"required": false
					}
			}]
		}
		}

		getDetailControls() {
			return   {
				width: 600,
				height: 300,
				controls: [{
					"label": "用户编号",
					"group": "基本信息",
					"type": "textbox",
					"name": "userNo",
					"options": {
						"inputWidth": 200,
						"required": false,
						"disabled": true
					}
				}, {
					"label": "姓名",
					"group": "基本信息",
					"type": "textbox",
					"name": "name",
					"options": {
						"inputWidth": 200,
						"required": true
					}
				}, {
					"label": "登录帐号",
					"group": "基本信息",
					"type": "textbox",
					"name": "account",
					"options": {
						"inputWidth": 200,
						"required": true
					}
				}, {
					"label": "密码",
					"group": "基本信息",
					"type": "password",
					"name": "password",
					"options": {
						"inputWidth": 200,
						"required": true
					}
				}]
			}
		}

		getGridOptions() {			
			var gender = ["男","女"];
			var options = {
				id: 'grid',
				title: "用户信息",
				height: "670",
				url: config.rootUrl + '/user/list',
				loadMsg: '加载中。。。',
				iconCls: 'icon-ok',
				pageSize: "20",
				pageList: [20, 50, 100, 200],
				checkOnSelect: false,
				pagination: true,
				fitColumns: false,
				singleSelect: false,
				rownumber: true,
				enableHeaderContextMenu: true,
				enableHeaderClickMenu: true,
				emptyMsg: "暂无数据",
				export: {
					async: true
				},
				columns: [
					[{
						"field": "userNo",
						"type": "textbox",
						"title": "用户编号",
						"width": 120,
						"hidden": false
					}, {
						"field": "id",
						"type": "textbox",
						"title": "",
						"width": 80,
						"hidden": true
					}, {
						"field": "name",
						"type": "textbox",
						"title": "姓名",
						"width": 120,
						"hidden": false
					}, {
						"field": "password",
						"type": "textbox",
						"title": "密码",
						"width": 80,
						"hidden": true
					}, {
						"field": "account",
						"type": "textbox",
						"title": "账号",
						"width": 80,
						"hidden": false
					}, {
						"field": "gender",
						"type": "textbox",
						"title": "性别",
						"formatter": (value) => {
							let item = gender[value];
							return item ;
						},
						"width": 60,
						"hidden": false
					}]
				]
			}; 
			return [options];
		}
		

		doSave() {
			let isNew = (this.data == null || this.data.rowIndex < 0);
			var data = this.getDetail();
			var def;

			if (!isNew)
				def = this.service.update(data.rowData)
				.then(this.saved);
			else {
				if (!this.validateDetail()) {
					def = $.Deferred();
					setTimeout(() => def.reject(), 10)
					return def;
				}
				def = this.service.create(data.rowData)
					.then(this.saved);
			}
			let self = this;

			return def.then(c => this.refreshRow(c));
		}

	}

	class Page extends UI.Page {
		constructor() {
			super('1110017', $('#mainPanel'));
			this.views = [new UserView()]
		}
	}

	var page = new Page();

	page.render();

});